'use strict';

const Service = require('egg').Service;

const {
    ERROR,
    SUCCESS,
} = require('../util/util');

class UserService extends Service {
   async login(mobilePhone, 
                openId,
                username, 
                avatarUrl,
                gender
            ) {
                
    const { ctx } = this

    try {
        const user = await ctx.model.User.findOne({
            where: {
                mobilePhone: mobilePhone
            }
        })
        
        if (!user) { // user不存在注册
            const adduser = await ctx.model.User.create({
                mobilePhone: mobilePhone,
                openId: openId,
                username: username,
                gender: gender,
                avatarUrl: avatarUrl
            })

            return Object.assign(SUCCESS(), {
                data: Object.assign(adduser),
            });
        }else {
            if (user.state === 1) {
                if (user.openId === openId) { // 用户存在登录成功
                    return Object.assign(SUCCESS(), {
                        data: Object.assign(user),
                    });
                }else {
                    return Object.assign(ERROR(), {
                        msg: '微信号和手机号不符',
                    });
                }
            }else {
                return Object.assign(ERROR(), {
                    msg: '用户被锁定，请联系管理员',
                });
            }
        }
    } catch (error) {
        logger.error(error)
        return Object.assign(ERROR(), {
            msg: '服务器错误',
        });
    }
   }

   async registered(mobilePhone, openId, username, sex, headPhoto) {
       const { ctx } = this

       try {
            const user = await ctx.model.User.findOne({
                where: {
                    mobilePhone: mobilePhone,
                    openId: openId
                }
            })

            if (user) { // user存在
                return Object.assign(ERROR(), {
                    msg: '注册失败，用户已存在请直接登录',
                });
            } 

            const adduser = await ctx.model.User.create({
                mobilePhone: mobilePhone,
                openId: openId,
                username: username,
                sex: sex,
                headPhoto: headPhoto
            })

            return Object.assign(SUCCESS(), {
                data: Object.assign(adduser),
            });
       } catch (error) {
            logger.error(error)
            return Object.assign(ERROR(), {
                msg: '服务器错误',
            });
       }
   }

   async refreshUser(username, headPhoto, sex) {
       const { ctx } = this
       try {
           const user = await ctx.model.User.update({
                username: username,
                headPhoto: headPhoto,
                sex: sex
           })

           
           if (user) {
                if (user.state === 1) {
                    return Object.assign(SUCCESS(), {
                        data: Object.assign(user),
                    });
                }else{
                    return Object.assign(ERROR(), {
                        msg: '该用户被锁定，请联系管理员',
                    });
                }
           }

           return Object.assign(ERROR(), {
               msg: '更新失败',
           });
       } catch (error) {
            logger.error(error)
            return Object.assign(ERROR(), {
                msg: '服务器错误',
            });
       } 
   }
}

module.exports = UserService;
